package com.king01.aspect;

import com.king01.entity.Staff;
import com.king01.entity.SysOperLog;
import com.king01.entity.SysOperLog02;
import com.king01.service.SysOperLogService;
import com.king01.service.SysOperLogService02;
import com.king01.vo.StaffExVo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Component
@Aspect
public class LogAspect02 {

    @Autowired
    private HttpSession session;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SysOperLogService02 sysOperLogService02;

    @Pointcut("@annotation(com.king01.annotation.LogAnnotation02)")
    private void mypoint(){}

    @Around("mypoint()")
    public Object my(ProceedingJoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println(args);
        StaffExVo staffExVo = new StaffExVo();
        for (Object arg : args) {
            staffExVo = (StaffExVo) arg;
        }
        System.out.println(staffExVo);
        SysOperLog02 sysOperLog02 = new SysOperLog02();

        sysOperLog02.setBranchid(staffExVo.getBranchid());
        sysOperLog02.setDeptid(staffExVo.getDeptid());
        sysOperLog02.setNewbranchid(staffExVo.getNewbranchid());
        sysOperLog02.setNewroleid(staffExVo.getRoleid());
        sysOperLog02.setNewdeptid(staffExVo.getNewdeptid());
        sysOperLog02.setStaffid(staffExVo.getStaffid());
        sysOperLog02.setRoleid(staffExVo.getRoleid());
        Staff attribute = (Staff)session.getAttribute("staff");
        sysOperLog02.setTransferper(attribute.getStaffid());
        sysOperLog02.setTransfertime(new Date());


        try {
            Object proceed = joinPoint.proceed();
            return proceed;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }finally {
            sysOperLogService02.insert(sysOperLog02);
        }


    }

}
